$(() => {
    const usp = new URLSearchParams(window.location.search)
    const id = usp.get('id')
    let state = ''
    // 分类
    // 获取文章类别
    const getListState = () => {
        ajax('GET', BigNew.category_list, {}, (res) => {
            const htmlStr = template('moban', res)
            console.log(htmlStr);
            $('#category').html(htmlStr)
        })
    }


    // 初始化日期
    const renderdate = () => {
        layui.use('laydate', function () {
            var laydate = layui.laydate;
            //执行一个laydate实例
            laydate.render({
                elem: '#articleDate' //指定元素
            });
        });
    }
    renderdate()

    // 初始化富文本编辑器
    tinymce.init({
        selector: '#articleContent'
        ,// 本地化配置 - language（界面语言）
        language: 'zh_CN',
    });

    // 发请求渲染
    const getEditList = () => {

        ajax('GET', BigNew.article_search, { id }, (res) => {
            console.log(res);
            if (res.code !== 200) {
                return layer.msg(res.msg)
            }
            $('#inputTitle').val(res.data.title)
            $('.article_cover').prop('src', res.data.cover)
            $('#inputTitle').val(res.data.title)
            $('#category').val(res.data.categoryId)
            $('#articleDate').val(res.data.date)
            $('#articleContent').val(res.data.content)

        })
    }


    getListState()
    getEditList()
    $('#form').on('submit', function (e) {
        e.preventDefault()
        const fd = new FormData(this)
        console.log(state);
        fd.forEach((value, key) => {
            console.log({ value, key });
        })
        fd.append('state', state)
        fd.append('id', id)

        ajax('POST', BigNew.article_edit, fd, (res) => {
            layer.msg(res.msg)
            window.location.href = 'article_list.html'
        })

    })

    $('.btn-edit').on('click', function () {
        state = '已发布'
    })
    $('#inputCover').on('change', function () {
        const ulr = URL.createObjectURL(this.files[0])
        console.log(ulr);
        $('.article_cover').prop('src', ulr)
    })





})
// {
//     "id": 225,
//     "title": "文章标题文字",
//     "cover": "http://localhost:8080/53de09b8563acbd78d42473796d39cb3",
//     "date": "2022-01-20",
//     "content": "<p>请输入文章内容</p>",
//     "state": "已发布",
//     "author": "管理员",
//     "categoryId": 1
// }
